//
//  CTPDetailListViewController.m
//  LittleNotes
//
//  Created by 檀志文 on 16/5/19.
//  Copyright © 2016年 ios檀志文. All rights reserved.
//

#import "CTPDetailListViewController.h"
#import "UIBarButtonItem+Extension.h"
#import "DWBubbleMenuButton.h"
#import "XXXRoundMenuButton.h"
#import "CTPComposeViewController.h"
#import "CTPNavViewController.h"
#import "CTPDetailTextTableViewCell.h"
#import "CTPMsgViewModel.h"

@interface CTPDetailListViewController ()
// 时间选择
@property (nonatomic, strong) UILabel *label;
// 发布选择
@property (nonatomic, strong) UILabel *labelType;

@property (nonatomic, strong) XXXRoundMenuButton *button2;

//messages的模型数组
@property (nonatomic, strong) NSMutableArray *messages;

@end

@implementation CTPDetailListViewController

// view已经显示的时候加载


- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
        [self refreshDatas];
}
- (void)viewDidAppear:(BOOL)animated {
    
    // Create down menu button
    UILabel *homeLabel = [self createHomeButtonView1];
    
    DWBubbleMenuButton *upMenuView = [[DWBubbleMenuButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width - homeLabel.frame.size.width - 20.f,
                                                                                          self.view.frame.size.height - homeLabel.frame.size.height - 20.f,
                                                                                          homeLabel.frame.size.width,
                                                                                          homeLabel.frame.size.height)
                                                            expansionDirection:DirectionDown];
    upMenuView.homeButtonView = homeLabel;
    
    [upMenuView addButtons:[self createDemoButtonArray:0]];
    
    self.navigationItem.title = @"列表";
    //    设置一个自定义按钮
    UIBarButtonItem *sLeftBarButton = [[UIBarButtonItem alloc]initWithCustomView:upMenuView];
    UIButton *button = [[UIButton alloc]init];
    button.frame = CGRectMake(0, 0, 22, 22);
    [button setBackgroundImage:[UIImage imageNamed:@"Search"] forState:UIControlStateNormal];
    //    设置一个自定义按钮
    UIBarButtonItem *sRightBarButton = [[UIBarButtonItem alloc]initWithCustomView:button];
    //   设置导航栏按钮
    self.navigationItem.leftBarButtonItems =@[sLeftBarButton,sRightBarButton];
    
//  去发布语音或者文字/图片说说
    homeLabel = [self createHomeButtonView2];
    DWBubbleMenuButton *downMenuButton = [[DWBubbleMenuButton alloc] initWithFrame:CGRectMake(20.f,20.f,homeLabel.frame.size.width,homeLabel.frame.size.height)
                                                                expansionDirection:DirectionLeft];
    downMenuButton.homeButtonView = homeLabel;
    
    [downMenuButton addButtons:[self createDemoButtonArray:1]];

    //    设置一个自定义按钮
    UIBarButtonItem *chooseBarButton = [[UIBarButtonItem alloc]initWithCustomView:downMenuButton];
    self.navigationItem.rightBarButtonItem = chooseBarButton;
}

#pragma mark - 查询按钮的设置
- (UILabel *)createHomeButtonView1 {
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0.f, 0.f, 45.f, 45.f)];
    label.text = @"请选择";
    label.textColor = [UIColor whiteColor];
    label.textAlignment = NSTextAlignmentCenter;
    label.layer.cornerRadius = label.frame.size.height / 2.f;
    label.backgroundColor = CMTGlobalColor;
    label.alpha = 0.5;
    label.font = [UIFont systemFontOfSize:10];
    label.clipsToBounds = YES;
    self.label =label;
    return label;
}
- (UILabel *)createHomeButtonView2 {
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0.f, 0.f, 45.f, 45.f)];
    label.text = @"请选择";
    label.textColor = [UIColor whiteColor];
    label.textAlignment = NSTextAlignmentCenter;
    label.layer.cornerRadius = label.frame.size.height / 2.f;
    label.backgroundColor = CMTGlobalColor;
    label.alpha = 0.5;
    label.font = [UIFont systemFontOfSize:10];
    label.clipsToBounds = YES;
        self.labelType=label;
    return label;
}

- (NSArray *)createDemoButtonArray:(NSInteger)type {
    
    NSMutableArray *buttonsMutable = [[NSMutableArray alloc] init];
    int i = 0;
//  判断类型，到底是否什么类型
    NSArray *dicArr=type ==0?@[@"本周", @"本月", @"一个月前"]:@[@"语音",@"文字/图片"];
    for (NSString *title in dicArr) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [button setTitle:title forState:UIControlStateNormal];
        
        button.frame = CGRectMake(0.f, 0.f, 45.f, 45.f);
        button.layer.cornerRadius = button.frame.size.height / 2.f;
        button.backgroundColor = CMTGlobalColor;
        button.clipsToBounds = YES;
        button.titleLabel.font=[UIFont systemFontOfSize:10];
        button.alpha=0.5;
        button.tag = i++;
        if (type==0) {
            [button addTarget:self action:@selector(searchByCondition:) forControlEvents:UIControlEventTouchUpInside];
        }else{
            [button addTarget:self action:@selector(composeType:) forControlEvents:UIControlEventTouchUpInside];
        }
        
        
        [buttonsMutable addObject:button];
    }
    return [buttonsMutable copy];
}

- (UIButton *)createButtonWithName:(NSString *)imageName {
    
    
    UIButton *button = [[UIButton alloc] init];
    [button setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    [button sizeToFit];
    
    [button addTarget:self action:@selector(searchByCondition:) forControlEvents:UIControlEventTouchUpInside];
    
    return button;
}

#pragma mark 根据年月日(查询)设置查询条件

- (void)searchByCondition:(UIButton *)sender {
    
//  根据查询条件查询相应的记事本
    
    self.label.text = sender.titleLabel.text;
    NSLog(@"Button tapped, tag: %ld", (long)sender.titleLabel.text);
}

#pragma mark 发布笔记的方式
-(void)composeType:(UIButton *)sender{
    self.labelType.text = sender.titleLabel.text;
    NSLog(@"Button tapped, tag: %ld", (long)sender.titleLabel.text);
//  调用方法跳转到发布说说界面
    CTPComposeViewController *compose = [[CTPComposeViewController alloc]init];
    [self.navigationController pushViewController:compose animated:YES];
    
}

- (BOOL)prefersStatusBarHidden {
    
    return true;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.tableView registerNib:[UINib nibWithNibName:@"CTPDetailTextTableViewCell" bundle:nil] forCellReuseIdentifier:@"text"];
    self.tableView.rowHeight= 100;
}

#pragma mark- 刷新数据
-(void) refreshDatas{
    //  1 先从缓存找
    //  2 没找到就去数据库中找
    _messages=[[[CTPMsgViewModel sharedMsgInstance]searchAllMsgs]copy];
    
    [self.tableView reloadData];
}


#pragma mark - 选择发布语音或者是文本/图片
-(void) didChooseYuYinOrTextImage{
        
}


#pragma mark - TableView 的数据源和代理方法


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.messages.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CTPDetailTextTableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:@"text" forIndexPath:indexPath];
    cell.message = self.messages[indexPath.row];
    
    return cell;
}

#pragma mark - 代理方法



#pragma mark - 查看详细单元格的信息

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"123");
    
}

#pragma mark - 懒加载
- (UILabel *)label{
    if (_label == nil) {
        _label = [[UILabel alloc] init];
    }
    return _label;
}
- (UILabel *)labelType{
    if (_labelType == nil) {
        _labelType = [[UILabel alloc] init];
    }
    return _labelType;
}

- (XXXRoundMenuButton *)button2{
    if (_button2 == nil) {
        _button2 = [[XXXRoundMenuButton alloc] init];
    }
    return _button2;
}

- (NSMutableArray *)messages{
    if (_messages == nil) {
        _messages = [[[CTPMsgViewModel sharedMsgInstance]searchAllMsgs] copy];
        ;
    }
    return _messages;
}

@end
